using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;


using GoblinV2;
using GoblinV2.GUI.GUI2D;
using GoblinV2.GUI.Events;
using TrueVision3D;
using DxVBLibA;

namespace Goblin2
{
    public class Stats
    {
        public int myMarkerArray1, myMarkerArray2, myMarkerArray3, myMarkerArray4, myMarkerArray5;
        public Manhattanville myForm;
        public TVMesh statSheet, statText;
        private TVMesh chart1Sheet, chart2Sheet, chart3Sheet, chart4Sheet, valueMesh, ageMesh, unitsMesh, floorsMesh;
        public static float SCALE = .1f;
        public static float SPACE = 1f;
        public int _x, _y;
        private ArrayList stats;
        private Hashtable buildingInfo;
        private TVTextureFactory m_kTexFactory;

        public Stats(Manhattanville f, int marker1, int marker2, int marker3, int marker4, int marker5, int x, int y, Hashtable ht)
        {
            m_kTexFactory = new TVTextureFactory();
            myMarkerArray1 = marker1;
            myMarkerArray2 = marker2;
            myMarkerArray3 = marker3;
            myMarkerArray4 = marker4;
            myMarkerArray5 = marker5;
            myForm = f;
            _x = x;
            _y = y;

            buildingInfo = ht;

            createCharts();
        }

        private void createCharts()
        {
            chart1Sheet = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Chart1");
            chart1Sheet.CreateBox(7.5f, 7.5f, .1f, false);
            chart1Sheet.SetColor(Color.Blue.ToArgb(), false);
            chart1Sheet.SetPosition(0, 0, 0);
            chart1Sheet.ForceMatrixUpdate();
            myForm.objects.Add(new TrackedObject(chart1Sheet, myMarkerArray2));

            valueMesh = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Value");
            valueMesh.CreateBox(7, 7, .2f, false);
            valueMesh.SetColor(Color.White.ToArgb(), false);
            valueMesh.SetPosition(0, 0, 0);
            valueMesh.SetRotation(0f, 0f, (float)(myForm.mathLib.PI_ / 2));

            valueMesh.ForceMatrixUpdate();
            chart1Sheet.AddChild(ref valueMesh);

            int map_texture = m_kTexFactory.LoadTexture("MarketValue.jpg", "Market Value", -1, -1,
                CONST_TV_COLORKEY.TV_COLORKEY_NO, false, false);

            valueMesh.SetTexture(map_texture, -1);

            chart2Sheet = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Chart2");
            chart2Sheet.CreateBox(7.5f, 7.5f, .1f, false);
            chart2Sheet.SetColor(Color.Blue.ToArgb(), false);
            chart2Sheet.SetPosition(0, 0, 0);
            chart2Sheet.ForceMatrixUpdate();
            myForm.objects.Add(new TrackedObject(chart2Sheet, myMarkerArray3));

            ageMesh = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Age");
            ageMesh.CreateBox(7, 7, .2f, false);
            ageMesh.SetColor(Color.White.ToArgb(), false);
            ageMesh.SetPosition(0, 0, 0);
            ageMesh.SetRotation(0f, 0f, (float)(myForm.mathLib.PI_ / 2));

            ageMesh.ForceMatrixUpdate();
            chart2Sheet.AddChild(ref ageMesh);

            map_texture = m_kTexFactory.LoadTexture("age.jpg", "Building Age", -1, -1,
                CONST_TV_COLORKEY.TV_COLORKEY_NO, false, false);

            ageMesh.SetTexture(map_texture, -1);

            chart3Sheet = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Chart3");
            chart3Sheet.CreateBox(7.5f, 7.5f, .1f, false);
            chart3Sheet.SetColor(Color.Blue.ToArgb(), false);
            chart3Sheet.SetPosition(0, 0, 0);
            chart3Sheet.ForceMatrixUpdate();
            myForm.objects.Add(new TrackedObject(chart3Sheet, myMarkerArray4));

            unitsMesh = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Unit");
            unitsMesh.CreateBox(7, 7, .2f, false);
            unitsMesh.SetColor(Color.White.ToArgb(), false);
            unitsMesh.SetPosition(0, 0, 0);
            unitsMesh.SetRotation(0f, 0f, (float)(myForm.mathLib.PI_ / 2));

            unitsMesh.ForceMatrixUpdate();
            chart3Sheet.AddChild(ref unitsMesh);

            map_texture = m_kTexFactory.LoadTexture("units.jpg", "Building Units", -1, -1,
                CONST_TV_COLORKEY.TV_COLORKEY_NO, false, false);

            unitsMesh.SetTexture(map_texture, -1);

            chart4Sheet = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Chart4");
            chart4Sheet.CreateBox(7.5f, 7.5f, .1f, false);
            chart4Sheet.SetColor(Color.Blue.ToArgb(), false);
            chart4Sheet.SetPosition(0, 0, 0);
            chart4Sheet.ForceMatrixUpdate();
            myForm.objects.Add(new TrackedObject(chart4Sheet, myMarkerArray5));

            floorsMesh = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Unit");
            floorsMesh.CreateBox(7, 7, .2f, false);
            floorsMesh.SetColor(Color.White.ToArgb(), false);
            floorsMesh.SetPosition(0, 0, 0);
            floorsMesh.SetRotation(0f, 0f, (float)(myForm.mathLib.PI_ / 2));

            floorsMesh.ForceMatrixUpdate();
            chart4Sheet.AddChild(ref floorsMesh);

            map_texture = m_kTexFactory.LoadTexture("floors.jpg", "Building Floors", -1, -1,
                CONST_TV_COLORKEY.TV_COLORKEY_NO, false, false);

            floorsMesh.SetTexture(map_texture, -1);

}

        /* setStats(l)
         * places all the items in list l into the stat sheet 
         * delimited by newline in the form of new text offset further down
         * note: font resizing not working
         */
        public void setStats(ArrayList l)
        {
            stats = new ArrayList();
            l.Sort();

            float offset = (l.Count * SPACE);
            statSheet = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("Sheet");
            statSheet.CreateBox(offset, 13f, .1f, false);
            statSheet.SetColor(Color.White.ToArgb(), false);
            statSheet.SetPosition(0, 0, 0);
            //statSheet.ScaleMesh(.75f, .75f, .75f);
            statSheet.ForceMatrixUpdate();
            myForm.objects.Add(new TrackedObject(statSheet, myMarkerArray1));
            offset *= -.35f;

            foreach (Object o in l)
            {
                TVMesh line = (TVMeshClass)myForm.m_kScenegraph.CreateMeshBuilder("text" + offset);
                line.Create3DText((String)o, "Arial", .5f, .2f, 0, true, false);
                line.SetColor(Color.Black.ToArgb(), false);
                line.SetRotation(0f, 0f, (float)(myForm.mathLib.PI_ / 2));
                //line.SetPosition(_y * SCALE - 5.5f + offset, _x * SCALE - 4f, 0);
                line.SetPosition(offset, -4.5f, 0);
                line.ForceMatrixUpdate();
                statSheet.AddChild(ref line);
                stats.Add(line);
                offset += SPACE;
                
            }
        }

        public void clearStats()
        {
            if (statSheet != null)
            {
                statSheet.DeleteChilds();
                myForm.m_kScenegraph.DestroyMesh(ref statSheet);
                TVMesh s = new TVMesh() ;

                for (int i = 0; i < stats.Count; i++)
                {
                    s = ((TVMesh)stats[i]);
                    myForm.m_kScenegraph.DestroyMesh(ref s);
                }
            }
        }

        public static ArrayList HT2List(Hashtable t)
        {
            ArrayList l = new ArrayList();

            foreach (object o in t.Keys)
            {
                l.Add(o + ": " + t[o]);
            }
            return l;
        }
    }
}
